package nextoffer;

/**
 * @author ：szq
 * @date ：Created in 2022/3/20 10:27
 * @description：剑指 Offer 35. 复杂链表的复制
 * @modified By：
 * @version: $
 */
public class replicationOfComplexLinkedLists {

// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

    class Solution {
        public Node copyRandomList(Node head) {
            Node dummy = head;
            while(dummy!=null)
            {
                Node tmp = new Node(dummy.val);
                tmp.next = dummy.next; //新建的临时结点

                dummy.next = tmp;   //旧的指向 新建的

                dummy = tmp.next;

            }
            Node rand = head;
            Node quick = rand.next;
            while(quick!=null)
            {
                if(rand.random!=null)
                    quick.random = rand.random.next;
                else
                    quick.random = null;

                quick = quick.next;

                rand = rand.next;

            }

            Node p = head;

            Node q = p.next;
            Node res = q;
            while(p!=null)
            { System.out.println(q.val);
                p = q.next;
                q = p.next;

            }

            return res;
        }
    }
}
